<template>
  <div class="ColumnManagement">
    <div class="ColumnManagement_title">
      <el-button
        type="primary"
        @click="addColumn"
        size="small"
      >添加内容管理</el-button>
      <el-form
        size="small"
        label-width="100px"
        :inline="true"
      >
        <el-form-item>
          <el-input
            v-model="tableFrom.cid"
            @keyup.enter.native="getList(1)"
            placeholder="请输入栏目id"
            class="selWidth"
            size="small"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              size="small"
              class="el-button-solt"
              @click="getList(1)"
            />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="tableFrom.title"
            @keyup.enter.native="getList(1)"
            placeholder="请输入内容标题"
            class="selWidth"
            size="small"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              size="small"
              class="el-button-solt"
              @click="getList(1)"
            />
          </el-input>
        </el-form-item>
      </el-form>

    </div>
    <el-table
      v-loading="listLoading"
      :data="tableData.data"
      style="width: 100%"
      size="small"
    >
      <el-table-column
        prop="cid"
        label="栏目id"
        width="70"
      >
      </el-table-column>
      <el-table-column
        prop="id"
        label="文章id"
        width="70"
      >
      </el-table-column>
      <el-table-column
        prop="title"
        label="文章名称"
        width="180"
      >
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="image_input"
        label="栏目封面图"
      >
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image
              style="width: 36px; height: 36px"
              :src="scope.row.image_input"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="120"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-text="显示"
            inactive-text="不显示"
            @change="onchangeIsShow(scope.row)"
          />

        </template>

      </el-table-column>
      <el-table-column
        prop="is_hot"
        label="是否热门"
        width="120"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.is_hot"
            :active-value="1"
            :inactive-value="0"
            active-text="显示"
            inactive-text="不显示"
            @change="onchangeIs_hot(scope.row)"
          />

        </template>

      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="100"
        fixed="right"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="updateAdd(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            @click="removeAdd(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :page-sizes="[20, 40, 60, 80]"
        :page-size="tableFrom.limit"
        :current-page="tableFrom.page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="pageChange"
      />
    </div>
    <AddUpData
      :addTitle="addTitle"
      :dialogVisible="dialogVisible"
      @dialogVisibleClick="dialogVisibleClick"
      :upDataId="upDataId"
    ></AddUpData>
  </div>
</template>

<script>
import AddUpData from './components/add'
import { contentList, contentDelete, contentStatus, contentIs_hot } from '@/api/column'
export default {
  name: 'MerchantClassify',
  components: {
    AddUpData
  },
  data() {
    return {
      tableData: {
        data: [],
        total: 0
      },
      listLoading: true,
      dialogVisible: false,
      addTitle: '添加内容',
      tableFrom: {
        page: 1,
        limit: 20,
        title: '',
        cid: ''
      },

      upDataId: {}
    }
  },

  mounted() {
    this.getList('')
  },
  methods: {
    onchangeIsShow(row) {
      console.log(row);
      contentStatus(row).then(res => {
        console.log(res, '修改状态');
        if (res.status == 200) {
          this.$message({
            message: '状态' + res.message,
            type: 'success'
          })
        }
      })
    },
    onchangeIs_hot(row) {
      contentIs_hot(row).then(res => {
        console.log(res, '修改状态');
        if (res.status == 200) {
          this.$message({
            message: '状态' + res.message,
            type: 'success'
          })
        }
      })
    },
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num ? num : this.tableFrom.page
      contentList(this.tableFrom).then((res) => {
        console.log(res, 'resres');
        if (res.status === 200) {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        }
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList('')
    },
    handleClick(row) {
      console.log(row);
    },
    addColumn() {
      this.dialogVisible = true;
    },
    updateAdd(row) {
      this.addTitle = '编辑内容'
      this.upDataId = {
        id: row.id,

      };
      this.dialogVisible = true;
    },
    dialogVisibleClick(row) {
      console.log(row, 'row');
      this.dialogVisible = row;
      this.getList('');
    },
    removeAdd(id) {
      contentDelete(id).then((res) => {
        console.log(res);
        if (res.status === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getList('');
        }
      }).catch(res => {
        this.$message.error(res.message)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
.ColumnManagement {
  padding: 20px 40px;
  box-sizing: border-box;
}
.ColumnManagement_title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
